package cn.iocoder.yudao.module.mqtt.service.devicereport;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.exception.ServiceException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.mqtt.controller.admin.devicereport.vo.*;
import cn.iocoder.yudao.module.mqtt.dal.dataobject.devicereport.DeviceReportDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.mqtt.dal.mysql.devicereport.DeviceReportMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.mqtt.enums.ErrorCodeConstants.*;

/**
 * MQTT报表分类 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class DeviceReportServiceImpl implements DeviceReportService {

    @Resource
    private DeviceReportMapper deviceReportMapper;

    @Override
    public Long createDeviceReport(DeviceReportSaveReqVO createReqVO) {
        validateDeviceDataTypeExists(createReqVO);
        // 插入
        DeviceReportDO deviceReport = BeanUtils.toBean(createReqVO, DeviceReportDO.class);
        deviceReportMapper.insert(deviceReport);
        // 返回
        return deviceReport.getId();
    }

    @Override
    public void updateDeviceReport(DeviceReportSaveReqVO updateReqVO) {
        validateDeviceDataTypeExists(updateReqVO);
        // 校验存在
        validateDeviceReportExists(updateReqVO.getId());
        // 更新
        DeviceReportDO updateObj = BeanUtils.toBean(updateReqVO, DeviceReportDO.class);
        deviceReportMapper.updateById(updateObj);
    }

    @Override
    public void deleteDeviceReport(Long id) {
        // 校验存在
        validateDeviceReportExists(id);
        // 删除
        deviceReportMapper.deleteById(id);
    }

    @Override
        public void deleteDeviceReportListByIds(List<Long> ids) {
        // 校验存在
        validateDeviceReportExists(ids);
        // 删除
        deviceReportMapper.deleteByIds(ids);
        }

    private void validateDeviceReportExists(List<Long> ids) {
        List<DeviceReportDO> list = deviceReportMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(DEVICE_REPORT_NOT_EXISTS);
        }
    }

    /**
     * 判断数据类型是否已存在
     * @param createReqVO
     */
    private void validateDeviceDataTypeExists(DeviceReportSaveReqVO createReqVO){
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("data_type", createReqVO.getDataType());
        if (deviceReportMapper.selectCount(queryWrapper)>0){
            throw exception(DEVICE_DATA_TYPE_EXISTS);
        }
    }
    private void validateDeviceReportExists(Long id) {
        if (deviceReportMapper.selectById(id) == null) {
            throw exception(DEVICE_REPORT_NOT_EXISTS);
        }
    }

    @Override
    public DeviceReportDO getDeviceReport(Long id) {
        return deviceReportMapper.selectById(id);
    }

    @Override
    public PageResult<DeviceReportDO> getDeviceReportPage(DeviceReportPageReqVO pageReqVO) {
        return deviceReportMapper.selectPage(pageReqVO);
    }

}